package com.yk.faceapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.allinpay.xed.R;
import com.yk.faceapplication.callback.LoanPurposeCallback;
import com.yk.faceapplication.entity.Purpose;

import java.util.List;


public class LoanPurposeAdapter extends BaseAdapter {
	private Context context = null;
	private List<Purpose> list = null;
	private LoanPurposeCallback callback;

	public LoanPurposeAdapter(Context context, List<Purpose> list,LoanPurposeCallback callback) {
		this.context = context;
		this.list = list;
		this.callback = callback;
	}

	@Override
	public int getCount() {
		if (list != null) return list.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (list != null) list.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_loan_term, null);
			viewHolder.optionTV = (TextView) convertView.findViewById(R.id.option_tv);
			convertView.setTag(viewHolder);
		}
		else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.optionTV.setText(list.get(position).getValue());
		if(callback != null){
			convertView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					callback.success(list.get(position));
				}
			});
		}
		return convertView;
	}

	class ViewHolder {
		private TextView optionTV = null;
	}

}
